﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using Ice.Toolkit.Data.ILInvoker;

namespace Ice.Toolkit.Data
{
    public abstract class DataTableHelper
    {
        public static Dictionary<String, Object> SingleToDictionary(DataTable dtbl, int index)
        {
            var dict = new Dictionary<String, Object>();

            if (dtbl.Rows.Count < index) return dict;

            foreach (DataColumn column in dtbl.Columns)
            {
                dict.Add(column.ColumnName, dtbl.Rows[index][column.ColumnName]);
            }

            return dict;
        }

        public static object[] FirstToArray(DataTable dtbl)
        {
            return SingleToArray(dtbl, 0);
        }

        public static object[] LastToArray(DataTable dtbl)
        {
            return SingleToArray(dtbl, dtbl.Rows.Count - 1);
        }

        public static object[] SingleToArray(DataTable dtbl, int index)
        {
            return SingleToDictionary(dtbl, index).Values.ToArray();
        }

        public static T SingleToObject<T>(DataTable dtbl, int index) where T : class, new()
        {
            T instance = new T();

            SingleToDictionary(dtbl, index).ToList().ForEach(o => PropertyAccessor<T>.Set(instance, o.Key, o.Value));

            return instance;
        }

        public static T FirstToObject<T>(DataTable dtbl) where T : class, new()
        {
            return SingleToObject<T>(dtbl, 0);
        }

        public static T LastToObject<T>(DataTable dtbl) where T : class, new()
        {
            return SingleToObject<T>(dtbl, dtbl.Rows.Count - 1);
        }

        public static List<T> AllToList<T>(DataTable dtbl) where T : class, new()
        { 
            List<T> list = new List<T>();

            if (dtbl.Rows.Count <= 0) return list;

            foreach (DataRow r in dtbl.Rows)
            {
                T instance = new T();

                foreach (DataColumn cln in dtbl.Columns)
                {
                    PropertyAccessor<T>.Set(instance, cln.ColumnName, r[cln]);
                }

                list.Add(instance);
            }

            return list;
        }

        public static void AddNewRow(DataTable dtbl, params object[] values)
        {
            DataRow dr = dtbl.NewRow();

            var length = dtbl.Columns.Count > values.Length ? values.Length : dtbl.Columns.Count;

            for (var i = 0; i < length; i++)
            {
                dr[i] = values[i];
            }

            dtbl.Rows.Add(dr);
        }

        public static void AddNewRow(DataTable dtbl, params KeyValuePair<String, Object>[] values)
        {
            DataRow dr = dtbl.NewRow();

            foreach (var v in values)
            {
                dr[v.Key] = v.Value;
            }

            dtbl.Rows.Add(dr);
        }

        public static void AddNewRow<T>(DataTable dtbl, T instance)
        {
            DataRow dr = dtbl.NewRow();

            foreach (DataColumn cln in dtbl.Columns)
            {
                dr[cln.ColumnName] = PropertyAccessor<T>.Get(instance, cln.ColumnName);
            }

            dtbl.Rows.Add(dr);
        }
    }
}
